﻿using Authority.DataTransferObject.Role;
using Authority.IServices;
using Authority.IServices.Model.Role;
using Authority.PresentationModels.Role;
using AutoMapper;
using HelperFromTheSkeleton.APICore;
using HelperFromTheSkeleton.Model.RequestModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Authority.WebAPI.Controllers
{
    /// <summary>
    /// 角色权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleAuthorityController : BaseController
    {

        private readonly IRoleAuthorityService  _roleAuthorityService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="RoleAuthorityService"></param>
        /// <param name="mapper"></param>
        public RoleAuthorityController(IRoleAuthorityService  RoleAuthorityService, IMapper mapper)
        {
            _roleAuthorityService = RoleAuthorityService;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Add(AddRoleRequestModel requestModel)
        {
            return await HandleAsync(async() =>
            {
                var model = _mapper.Map<AddRoleModel>(requestModel);
                await _roleAuthorityService.AddAsync(model);
            });
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Edit(EditRoleRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<EditRoleModel>(requestModel);
                await _roleAuthorityService.EditAsync(model);
            });
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel> Delete([Required]Guid id)
        {
            return await HandleAsync(async () =>
            {
                await _roleAuthorityService.DeleteAsync(id);
            });
        }

        /// <summary>
        /// 获取角色详情
        /// </summary>
        /// <param name="ids">唯一标识集</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<RoleAuthorityDTO>>> Get([Required] List<Guid> ids)
        {
            return await HandleAsync(async () =>
            {
                List<RoleAuthorityDTO> result = await _roleAuthorityService.GetAsync(ids);
                return result;
            });
        }

        /// <summary>
        /// 获取角色详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<RoleAuthorityNextLevelListDTO>>> GetOneLevel(Guid? parnetID)
        {
            return await HandleAsync(async () =>
            {
                List<RoleAuthorityNextLevelListDTO> result = await _roleAuthorityService.GetOneLevelAsync(parnetID);
                return result;
            });
        }

        /// <summary>
        /// 获取角色树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<RoleTreeDTO>>> GetTree()
        {
            return await HandleAsync(async () =>
            {
                List<RoleTreeDTO> result = await _roleAuthorityService.GetTreeAsync();
                return result;
            });
        }

    }
}
